<template>
	<view class="app-tabbar-index" :style="[AppSkinVar]">
		<!-- #ifndef APP -->
		<entrance-view />
		<!-- #endif -->
		<!-- #ifdef APP -->
		<home-card index="0" @endRefresh="stopDownRefresh" @tabbarChange="tabbarChange" @setPageHeaderColor="setTabbarHeaderColor"></home-card>
		<upgrade :isAuto="true"></upgrade>
		<!-- #endif -->
	</view>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { onLoad, onShow, onHide, onBackPress, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app';
import { useCurrentInstance } from '@/common/hooks';
import { useInit, useBasics } from '@/common/stores';
import { storeToRefs } from 'pinia';
import customTabbar from './utils/customTabbar';
import { useTabbarHeader } from './utils/hooks';
import { useI18n } from 'vue-i18n';
import { TABBAR_TXTS } from '@/env';
// #ifdef APP
import homeCard from './child/home.vue';
// #endif
// #ifndef APP
import entranceView from './child/entrance.vue';
// #endif
import {checkReleaseTips} from '@/common/request/api'

const ctx = useCurrentInstance();

const { t } = useI18n();

const { tabbarData, tabbarPageIndex } = storeToRefs(useInit());
const { hasInvitation } = storeToRefs(useBasics());

const tabbarChange = (index: number) => {
	tabbarPageIndex.value = index;
};

const { setTabbarHeaderColor } = useTabbarHeader(tabbarPageIndex);

onLoad(() => {
	// #ifdef APP
	uni.hideTabBar();
	const subNVue = uni.getSubNVueById('appTabbar');
	subNVue.show();
	// #endif

	let index = uni.getStorageSync('tabbarPageIndex');
	if (index) {
		tabbarPageIndex.value = index;
		uni.removeStorageSync('tabbarPageIndex');
	}
	const title = tabbarData.value[tabbarPageIndex.value]?.text;
	title&&uni.setNavigationBarTitle({
		title: TABBAR_TXTS.includes(title) ? t(`tabbar.${title}`) : title
	});
	
	// 如果存在结算提示，每天弹一次框
	checkReleaseTips().then(res => {
		if (res.code == 200 && res.data.status === 0 && res.data.expireTips) {
			const releaseTipsDay = uni.getStorageSync('releaseTipsDay')
			const dd = new Date().getTime()
			let compareDay
			if (releaseTipsDay) {
				const oneDayTime = 24 * 60 * 60 * 1000
				compareDay = Number(releaseTipsDay) + oneDayTime
			}
			if (!releaseTipsDay || dd > compareDay) {
				uni.showModal({
					title:'提示',
					content:res.data.expireTips,
					showCancel:false,
					success() {
						uni.setStorageSync('releaseTipsDay',dd)
					}
				})
			}
			
		}
	})
});

const stopDownRefresh = () => {
	// 停止下拉动作
	uni.stopPullDownRefresh();
};

// #ifndef APP
onShow(() => {
	customTabbar.onShow(`${tabbarPageIndex.value}`);
});
onHide(() => {
	customTabbar.onHide();
});
// 监听页面返回
onBackPress(() => {
	customTabbar.onShow(`${tabbarPageIndex.value}`);
});

// #endif
onReachBottom(() => {
	customTabbar.onReachBottom(`${tabbarPageIndex.value}`);
});
onPullDownRefresh(() => {
	customTabbar.onPullDownRefresh(`${tabbarPageIndex.value}`);
});
</script>

<style lang="scss">
page {
	background-color: #f7f7f7;
}
@include appBem_b(tabbar-index) {
	// #ifdef APP
	padding-bottom: 50px;
	// #endif
}
</style>
